package test.处理;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-5-17 20:13
 */
public class 输出老板全部准确的pm发布年规整 {

    @SneakyThrows
    public static void main(String[] args) {

        String dbNameMy = "gcr_pm_pub_year";

        int breakSize = 1000000;

        SmallTool.printMemory();
        DataSource ds = DSFactory.get("local_myjia");
        Db use3307 = Db.use(ds);
        String allDate = "select max(pmid) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));

        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始页码++" + i + "++id++" + start + "结束id++++" + end);
            String meSql = "select pmid, text, year  from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryMe = use3307.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer pmid = pmDeatil.getInt("pmid");
                String year = pmDeatil.getStr("year");
                String text = pmDeatil.getStr("text");
                Entity where = new Entity();
                where.setTableName(dbNameMy);
                where.set("pmid", pmid);
                if (StringUtils.isNotEmpty(year)) {
                    // 校验用
                    if (year.length() == 0) {
                        SmallTool.printMessError("year++" + year + "++pmid++" + pmid);
                    }
//                    if (year.contains("-")) {
//                        String[] split = year.split("-");
//                        if (split[0].length() == 4 && split[1].length() == 4) {
//                            year = split[0];
//                            pmDeatil.set("year", year);
//                            try {
//                                use3307.update(pmDeatil,where);
//                            } catch (SQLException throwables) {
//                                throwables.printStackTrace();
//                            }
//
//                        }
//                    }
                    // year++1994-94++pmid++11362189 这样特殊处理
//                    if (year.contains("-")) {
//                        String[] split = year.split("-");
//                        if (split[0].length() >=1 && split[1].length() >=1) {
//                            year = split[0];
//                            pmDeatil.set("year", year);
//                            try {
//                                use3307.update(pmDeatil, where);
//                            } catch (SQLException throwables) {
//                                throwables.printStackTrace();
//                            }
//
//                        }
//                    }
                    // year++ 1994 - 94++ pmid++ 11362189 这样特殊处理
//                    if (year.endsWith(".") || year.endsWith(",")) {
//                        year = year.substring(0, year.length() - 1);
//                        pmDeatil.set("year", year);
//                        try {
//                            use3307.update(pmDeatil, where);
//                        } catch (SQLException throwables) {
//                            throwables.printStackTrace();
//                        }
//                    }
//                    if (year.equals("0")) {
//                        if (text.contains(";")) {
//                            year = text.split(";")[0].split(" ")[0];
//                            pmDeatil.set("year", year);
//                            try {
//                                use3307.update(pmDeatil, where);
//                            } catch (SQLException throwables) {
//                                throwables.printStackTrace();
//                            }
//                        }
//                    }
                } else {
                    SmallTool.printMess("year 是空++pmid++" + pmid);
                }
            });
        }
    }
}